require 'socket'
require 'timeout'

class AgentTcp
  attr_reader :ip, :port, :socket
  
  def initialize(ip, port)
    @ip = ip;
    @port = port
  end
  
  def ping
    _connect('PING').chomp
  end
  
  def ram
    _connect('RAM').chomp
  end
  
  def mpuse(mountpoint)
    _connect('MPUSE ' + mountpoint).chomp
  end
  
private
  def _connect(cmd)
    Timeout::timeout(2) do
      @socket = TCPSocket.new(@ip, @port)
      @socket.puts cmd
      res = @socket.readline
      @socket.puts 'QUIT'
      @socket.close
      res
    end
  end
  
end
